<?php
/**
 * Redis的公用集成类
 * 
 * 
 * @date 2013-01-20
 * @author zhenbo.yu
 * @version 1.0
 */
class ZDoRedis {
    public $redis;
    public $ip = '192.168.1.15';
   // public $ip='117.79.228.71';
    public $ip_port = 6379;
    
    //用户关注的用户（@UserAA 用户替换的AA号)
    public $meToUser="User::@UserAA:MeToUser";
    
    public function __construct(){
        $this->redis = new Redis();
        $this->redis->connect($this->ip,$this->ip_port); 
    }
    

    /**
     * 获取所有Redis的Keys值
     * 
     * @param type $match
     * @return type 
     * 
     * @date 2013-01-20
     * @author zhenbo.yu
     * @version 1.0
     */
    public function getRedisKeys($match="*"){
        $allKeys = $this->redis->keys($match);  
        return $allKeys;
    }
    
    /**
     * 删除Redis的Keys值
     * 
     * @param type $match 
     * 
     * @date 2013-01-20
     * @author zhenbo.yu
     * @version 1.0
     */
    public function deleteRedisKeys($match="*"){
        $allKeys = $this->redis->keys($match); 
        foreach ($allKeys as $value) {
            $this->redis->delete($value);
        }
    }

    /**
     * 存储学校老师对应选择的班级
     * 
     * @param $aa_user_sn
     * @param $aa_class_sn
     * @param $aa_school_sn
     * 
     * @date 2013-01-21
     * @author wjy
     * @version 1.0
     * */
    public function setTeacherClass($aa_user_sn,$aa_class_sn,$aa_school_sn)
    {
    	if(!$aa_user_sn || !$aa_class_sn || !$aa_school_sn) return null;
    	
    	$key = str_replace(array('@SchoolAA','@UserAA'),array($aa_school_sn,$aa_user_sn),$this->userToClass);
    	
    	return $this->redis->set($key,$aa_class_sn);
    }
    
    /**
     * 存储学校家长对应的选择的孩子
     * 
     * @param $aa_user_sn
     * @param $aa_school_sn
     * @param $c_aa_user_sn
     * 
     * @date 2013-01-21
     * @author wjy
     * @version 1.0
     * 
     * */
    public function setParentChild($aa_user_sn,$c_aa_user_sn,$aa_school_sn)
    {
    	if(!$aa_user_sn || !$c_aa_user_sn || !$aa_school_sn) return null;
    	
    	$key = str_replace(array('@SchoolAA','@UserAA'),array($aa_school_sn,$aa_user_sn),$this->userToChild);
    	
    	return $this->redis->set($key,$c_aa_user_sn);
    }
    
    /**
     * 得到存储的redis值
     * 
     * @param $key
     * 
     * @date 2013-01-21
     * @author jinyue.wang
     * @version 1.0
     * 
     * */
    public function getUserKeyVal($key){
    	
    	if(empty($key)) return null;
    	
    	return $this->redis->get($key);
    }
    
    /**
     * 存储redis值
     * 
     * @param $key 
     * @param $aa_user_sn
     * 
     * @date 2013-01-21
     * @author wjy
     * @version 1.0
     * */
    public function setUserKeyVal($key,$aa_user_sn)
    {
    	if(empty($key)) return null;
    	
    	return $this->redis->set($key,$aa_user_sn);
    }
    
    /**
     * 删除aa号
     * 
     * @param $key
     * 
     * @date 2013-01-21
     * @author wjy
     * @version 1.0
     * 
     * */
    public function delUser($key)
    {
    	if(empty($key)) return null;
    	
    	return $this->redis->del($key);
    }
    public function get($key){
    	if(empty($key)) return null;
    	return $this->redis->get($key);
    }
    public function set($key,$value){
    	if(empty($key)) return null;
    	return $this->redis->set($key,$value);
    }
    public function del($key){
    	if(empty($key)) return null;
    	return $this->redis->del($key);
    }
}

